package com.yjf.ljr.view;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.yjf.ljr.R;
import com.yjf.ljr.activity.RedPacketActivity;
import com.yjf.ljr.dataclass.RedPacketDataClass.RedPacketInfo;

public class RedPacketDialog extends Dialog{

	private Context mContext;
	
	private TextView redPacketContent;
	private TextView redPacketImgText;
	private TextView redPpacketExperienceImgText;
//	private Button viewRedPacket;
	
	private Button cancelRedPacket;
	
	public RedPacketDialog(Context context) {
		super(context,R.style.DeclareDialog);
		mContext = context;	
		initView();
	}

	private void initView() {
		setContentView(R.layout.dialog_red_packet);
		redPacketContent = (TextView)findViewById(R.id.red_packet_content);
		redPacketImgText = (TextView)findViewById(R.id.red_packet_img);
		redPpacketExperienceImgText = (TextView)findViewById(R.id.red_packet_experience_img);
//		viewRedPacket = (Button)findViewById(R.id.view_red_packet);
//		viewRedPacket.setOnClickListener(new View.OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				dismiss();
//				Intent intent = new Intent();
//				intent.setClass(mContext, RedPacketActivity.class);
//				mContext.startActivity(intent);				
//			}
//		});
		cancelRedPacket = (Button)findViewById(R.id.view_red_cancel);
		cancelRedPacket.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss();			
			}
		});
	}
	
	public void setRedPacketContent(RedPacketInfo redPacketInfo) {
		if(redPacketInfo != null) {
			String amount = "0";
			if(!TextUtils.isEmpty(redPacketInfo.amount)) {
				amount = redPacketInfo.amount;
			}
			String titleContent = mContext.getString(R.string.red_packet_dialog_title_content, amount);	
			SpannableStringBuilder style = new SpannableStringBuilder(titleContent);
			style.setSpan(new ForegroundColorSpan(Color.RED), 6, 7, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
			redPacketContent.setText(style);
			DecimalFormat myFormatter = new DecimalFormat("##0.00");
			String amountFormat = amount;
			try{
				amountFormat = myFormatter.format(Double.parseDouble(amount));
			} catch (Exception e) {
				e.printStackTrace();
			}
			redPacketImgText.setText("￥"+amountFormat);
		}
	}
	
	public void setRedPacketMessage(String message) {
		if (message == null) {
			message = "";
		}

//		if (message.contains("体验金")) {
//			redPpacketExperienceImgText.setVisibility(View.VISIBLE);
//		} else {
//			redPpacketExperienceImgText.setVisibility(View.GONE);
//		}
//		if (message.contains("红包")) {
//			redPacketImgText.setVisibility(View.VISIBLE);
//		} else {
//			redPacketImgText.setVisibility(View.GONE);
//		}

		String content = mContext.getResources().getString(R.string.red_packet_dialog_title_content, message);
		SpannableString ss = new SpannableString(content);

//		Pattern pattern = Pattern.compile("\\d+,?|\\d+.?|\\d+");
		Pattern pattern = Pattern.compile("\\d+\\.\\d+|\\d+,?");
		Matcher matcher = pattern.matcher(content);
		ArrayList<String> lists = new ArrayList<String>();
		while (matcher.find()) {
			String temp = matcher.group(0);
			lists.add(temp);
		}
		int temp = 0;
		for (String test : lists) {
			int start = content.indexOf(test);
			int end = start + test.length();
			temp = temp + start;
			content = content.substring(end);
			ss.setSpan(new ForegroundColorSpan(Color.RED), temp, temp + test.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			temp = temp + test.length();
		}
		redPacketContent.setText(ss);
		
//		redPacketContent.setText(mContext.getResources().getString(R.string.red_packet_dialog_title_content,message));
	}
	
}
